package com.top75;

import com.bean.TreeNode;

import java.util.*;

/**
 * @Author: huangzhigao
 * @Date: 2023/5/23 20:45
 */
public class Code199 {
    public List<Integer> rightSideView(TreeNode root) {
        if (root == null) {
            return Collections.EMPTY_LIST;
        }
        List<Integer> list = new ArrayList<>();
        list.add(root.val);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (i == size - 1) {
                    list.add(poll.val);
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return list;
    }
}
